Trò chơi Tic-Tac-Toe, game đánh caro full source code
53.574 lượt xem;
1 using System;
2 using UnityEngine;
3
4 namespace Assets.Scripts
5 {
6 public class Board : CoreBehaviour
7 {
8 private const int ROWS = 3;
9 private const int COLS = 3;
10
11 public GameObject cellPrefab;
12 public float cellOffset = 1.5f;
13
14 private Cell[,] cells = new Cell[ROWS, COLS];
15
16 private Cell lastChangedCell;
17
18 private Seed currentPlayer;
19
20 private event Action<Seed, int, int> onBoardChange;
21
22 public void Init(Action<Seed, int, int> onBoardChangeAction)
23 {
24 CreateCells();
25 SetPlayer(Seed.Empty);
26 onBoardChange = onBoardChangeAction;
27 }
28
29 public void SetPlayer(Seed seed)
30 {
31 currentPlayer = seed;
32 }
33
34 public void SetCell(Seed seed, int row, int col)
35 {
36 SetPlayer(seed);
37 OnCellClick(cells[row, col]);
38 }
39
40 public void Clear()
41 {
42 for (int row = 0; row < ROWS; row++)
43 {
44 for (int col = 0; col < COLS; col++)
45 {
46 cells[row, col].Clear();
47 }
48 }
49
50 lastChangedCell = null;
51 }
52
53 public bool IsDraw()
54 {
55 for (int row = 0; row < ROWS; row++)
56 {
57 for (int col = 0; col < COLS; col++)
58 {
59 if (cells[row, col].Content == Seed.Empty)
60 {
61 return false;
62 }
63 }
64 }
65
66 return true;
67 }
68
69 public bool HasWon(Seed seed)
70 {
71 if (lastChangedCell == null)
72 {
73 return false;
74 }
75
76 return HasWonInTheRow(seed)
77 || HasWonInTheColumn(seed)
78 || HasWonInTheDiagonal(seed)
79 || HasWonInTheOppositeDiagonal(seed);
80 }
81
82 private bool HasWonInTheRow(Seed seed)
83 {
84 int row = lastChangedCell.Row;
85
86 return cells[row, 0].HasSeed(seed)
87 && cells[row, 1].HasSeed(seed)
88 && cells[row, 2].HasSeed(seed);
89 }
90
91 private bool HasWonInTheColumn(Seed seed)
92 {
93 int col = lastChangedCell.Col;
94
95 return cells[0, col].HasSeed(seed)
96 && cells[1, col].HasSeed(seed)
97 && cells[2, col].HasSeed(seed);
98 }
99
100 private bool HasWonInTheDiagonal(Seed seed)
101 {
102 return cells[0, 0].HasSeed(seed)
103 && cells[1, 1].HasSeed(seed)
104 && cells[2, 2].HasSeed(seed);
105 }
106
107 private bool HasWonInTheOppositeDiagonal(Seed seed)
108 {
109 return cells[0, 2].HasSeed(seed)
110 && cells[1, 1].HasSeed(seed)
111 && cells[2, 0].HasSeed(seed);
112 }
113
114 private void OnCellClick(Cell cell)
115 {
116 if (currentPlayer != Seed.Empty && cell.IsEmpty)
117 {
118 cell.Set(currentPlayer);
119 lastChangedCell = cell;
120
121 if (onBoardChange != null)
122 {
123 onBoardChange(currentPlayer, cell.Row, cell.Col);
124 }
125 }
126 }
127
128 private void CreateCells()
129 {
130 Transform container = (new GameObject("Cells")).transform;
131
132 container.transform.parent = transform;
133 container.transform.localPosition = new Vector3(-cellOffset, -cellOffset, 0f);
134
135 for (int row = 0; row < ROWS; row++)
136 {
137 for (int col = 0; col < COLS; col++)
138 {
139 GameObject go = (GameObject)Instantiate(cellPrefab, Vector3.zero, Quaternion.identity);
140 Cell cell = go.GetComponent<Cell>();
141
142 cell.transform.parent = container;
143 cell.transform.localPosition = new Vector3(col * cellOffset, row * cellOffset, 0f);
144
145 cell.Init(row, col, OnCellClick);
146
147 cells[row, col] = cell;
148 }
149 }
150 }
151 }
152 }
2 using UnityEngine;
3
4 namespace Assets.Scripts
5 {
6 public class Board : CoreBehaviour
7 {
8 private const int ROWS = 3;
9 private const int COLS = 3;
10
11 public GameObject cellPrefab;
12 public float cellOffset = 1.5f;
13
14 private Cell[,] cells = new Cell[ROWS, COLS];
15
16 private Cell lastChangedCell;
17
18 private Seed currentPlayer;
19
20 private event Action<Seed, int, int> onBoardChange;
21
22 public void Init(Action<Seed, int, int> onBoardChangeAction)
23 {
24 CreateCells();
25 SetPlayer(Seed.Empty);
26 onBoardChange = onBoardChangeAction;
27 }
28
29 public void SetPlayer(Seed seed)
30 {
31 currentPlayer = seed;
32 }
33
34 public void SetCell(Seed seed, int row, int col)
35 {
36 SetPlayer(seed);
37 OnCellClick(cells[row, col]);
38 }
39
40 public void Clear()
41 {
42 for (int row = 0; row < ROWS; row++)
43 {
44 for (int col = 0; col < COLS; col++)
45 {
46 cells[row, col].Clear();
47 }
48 }
49
50 lastChangedCell = null;
51 }
52
53 public bool IsDraw()
54 {
55 for (int row = 0; row < ROWS; row++)
56 {
57 for (int col = 0; col < COLS; col++)
58 {
59 if (cells[row, col].Content == Seed.Empty)
60 {
61 return false;
62 }
63 }
64 }
65
66 return true;
67 }
68
69 public bool HasWon(Seed seed)
70 {
71 if (lastChangedCell == null)
72 {
73 return false;
74 }
75
76 return HasWonInTheRow(seed)
77 || HasWonInTheColumn(seed)
78 || HasWonInTheDiagonal(seed)
79 || HasWonInTheOppositeDiagonal(seed);
80 }
81
82 private bool HasWonInTheRow(Seed seed)
83 {
84 int row = lastChangedCell.Row;
85
86 return cells[row, 0].HasSeed(seed)
87 && cells[row, 1].HasSeed(seed)
88 && cells[row, 2].HasSeed(seed);
89 }
90
91 private bool HasWonInTheColumn(Seed seed)
92 {
93 int col = lastChangedCell.Col;
94
95 return cells[0, col].HasSeed(seed)
96 && cells[1, col].HasSeed(seed)
97 && cells[2, col].HasSeed(seed);
98 }
99
100 private bool HasWonInTheDiagonal(Seed seed)
101 {
102 return cells[0, 0].HasSeed(seed)
103 && cells[1, 1].HasSeed(seed)
104 && cells[2, 2].HasSeed(seed);
105 }
106
107 private bool HasWonInTheOppositeDiagonal(Seed seed)
108 {
109 return cells[0, 2].HasSeed(seed)
110 && cells[1, 1].HasSeed(seed)
111 && cells[2, 0].HasSeed(seed);
112 }
113
114 private void OnCellClick(Cell cell)
115 {
116 if (currentPlayer != Seed.Empty && cell.IsEmpty)
117 {
118 cell.Set(currentPlayer);
119 lastChangedCell = cell;
120
121 if (onBoardChange != null)
122 {
123 onBoardChange(currentPlayer, cell.Row, cell.Col);
124 }
125 }
126 }
127
128 private void CreateCells()
129 {
130 Transform container = (new GameObject("Cells")).transform;
131
132 container.transform.parent = transform;
133 container.transform.localPosition = new Vector3(-cellOffset, -cellOffset, 0f);
134
135 for (int row = 0; row < ROWS; row++)
136 {
137 for (int col = 0; col < COLS; col++)
138 {
139 GameObject go = (GameObject)Instantiate(cellPrefab, Vector3.zero, Quaternion.identity);
140 Cell cell = go.GetComponent<Cell>();
141
142 cell.transform.parent = container;
143 cell.transform.localPosition = new Vector3(col * cellOffset, row * cellOffset, 0f);
144
145 cell.Init(row, col, OnCellClick);
146
147 cells[row, col] = cell;
148 }
149 }
150 }
151 }
152 }